package com.sugar.psych.assessment.module.business.service.feedback;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.sugar.psych.assessment.module.business.controller.admin.feedback.vo.*;
import com.sugar.psych.assessment.module.business.dal.dataobject.feedback.FeedbackDO;
import com.sugar.psych.assessment.framework.common.pojo.PageResult;
import com.sugar.psych.assessment.framework.common.util.object.BeanUtils;

import com.sugar.psych.assessment.module.business.dal.mysql.feedback.FeedbackMapper;

import static com.sugar.psych.assessment.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.sugar.psych.assessment.module.business.enums.ErrorCodeConstants.*;

/**
 * 意见反馈 Service 实现类
 *
 * @author Ysugar
 */
@Service
@Validated
public class FeedbackServiceImpl implements FeedbackService {

    @Resource
    private FeedbackMapper feedbackMapper;

    @Override
    public Long createFeedback(FeedbackSaveReqVO createReqVO, Long userId) {
        // 插入
        FeedbackDO feedback = BeanUtils.toBean(createReqVO, FeedbackDO.class);
        feedback.setUserId(userId);
        feedbackMapper.insert(feedback);
        // 返回
        return feedback.getId();
    }

    @Override
    public void deleteFeedback(Long id) {
        // 校验存在
        validateFeedbackExists(id);
        // 删除
        feedbackMapper.deleteById(id);
    }

    @Override
        public void deleteFeedbackListByIds(List<Long> ids) {
        // 校验存在
        validateFeedbackExists(ids);
        // 删除
        feedbackMapper.deleteByIds(ids);
        }

    private void validateFeedbackExists(List<Long> ids) {
        List<FeedbackDO> list = feedbackMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(FEEDBACK_NOT_EXISTS);
        }
    }

    private void validateFeedbackExists(Long id) {
        if (feedbackMapper.selectById(id) == null) {
            throw exception(FEEDBACK_NOT_EXISTS);
        }
    }

    @Override
    public FeedbackDO getFeedback(Long id) {
        return feedbackMapper.selectById(id);
    }

    @Override
    public PageResult<FeedbackRespVO> getFeedbackPage(FeedbackPageReqVO pageReqVO) {
        return feedbackMapper.selectPage(pageReqVO);
    }

}